package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.beat_x3.Adapter.InstrumentAdapter;

import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import Entity.Equipments;
import Entity.Inf;

public class InstrumentFragment extends MyFragment {

    public InstrumentAdapter instrumentAdapter;
    public boolean isUpload = false;
    public Handler myHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                instrumentAdapter.refresh();
            } else if (msg.what == 1) {
                instrumentAdapter.remove((int) msg.obj);
                Toast.makeText(Inf.context, "完成借用，可前往用户中心查看", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @SuppressLint("InflateParams")
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_instrument, null);
        view.findViewById(R.id.instrument_myEquipment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.isLog) {
                    if (Inf.myinstrumentFragment == null) {
                        Inf.myinstrumentFragment = new MyInstrumentFragment();
                    }
                    Inf.myinstrumentFragment.isRefresh = false;
                    changeFragment(Inf.myinstrumentFragment);
                    if (isUpload) {
                        if (Inf.myinstrumentFragment.myInstrumentAdapter != null) {
                            Inf.myinstrumentFragment.myInstrumentAdapter.appendNew();
                        }
                        isUpload = false;
                    }
                } else Toast.makeText(Inf.context, "请登录", Toast.LENGTH_SHORT).show();
            }
        });
        ImageView imageView = view.findViewById(R.id.instrument_lend);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.isLog) {
                    if (Inf.chujieFragment == null) Inf.chujieFragment = new ChujieFragment();
                    changeFragment(Inf.chujieFragment);
                } else Toast.makeText(Inf.context, "请登录", Toast.LENGTH_SHORT).show();
            }
        });
        RecyclerView recyclerView = view.findViewById(R.id.instrument_re1);
        instrumentAdapter = new InstrumentAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(Inf.context));
        recyclerView.setAdapter(instrumentAdapter);
        instrumentAdapter.setOnItemClickListener(new InstrumentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String equipmentNumber, int position, int span) {
                if (Inf.isLog) {
                    if (Inf.stuInf.BorrowStatus == 0) {
                        Toast.makeText(Inf.context, "信用度低，禁止借用", Toast.LENGTH_SHORT).show();
                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Inf.context);
                        builder.setTitle("提示");
                        builder.setMessage("确定要借用该乐器吗？");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (GetResult.ReserveEquipmentGR(equipmentNumber, Inf.stuInf.StudentAccount, span) == 1) {
                                            Message message = new Message();
                                            message.what = 1;
                                            message.obj = position;
                                            myHandler.sendMessage(message);
                                        }
                                    }
                                }).start();
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        });
                        builder.show();
                    }
                }
            }
        });
        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.instrument_refresh);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        new InstrumentThread().start();
        return view;
    }

    public void refresh() {
        new InstrumentThread().start();
    }

    class InstrumentThread extends Thread {
        @Override
        public void run() {
            Equipments res = GetResult.QueryEquipments(0, null);
            if (res != null) {
                instrumentAdapter.equipments = res.equipments;
                myHandler.sendEmptyMessage(0);
            }
        }
    }
}
